﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Zhao.Common.Helper;
using Zhao.IServer.Log;
using Zhao.Models.DTO;
using Zhao.Models.Entity;

namespace Zhao.Arena.Controllers.Log
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class LogsController : ControllerBase
    {
        private readonly ILogsServer _logs;
        public LogsController(ILogsServer logs)
        {
            _logs = logs;
        }

        [HttpGet]
        [Route("{pageIndex:int}/{pageSize:int}")]
        [Route("{pageIndex:int}/{pageSize:int}/{level}")]
        [Route("{pageIndex:int}/{pageSize:int}/{level}/{searchaString}")]
        public async Task<JsonResult> GetLogsPageInfo(string searchaString="", string level="",int pageIndex = 1, int pageSize = 10)
        {
            var result = new PageModel<Logs>();

            Expressionable<Logs> expressionable = new Expressionable<Logs>();

            expressionable.AndIF(!level.IsNullOrWhiteSpace(), a => a.LogLevel.Contains(level) || a.LogContent.Contains(level));

            expressionable.AndIF(!searchaString.IsNullOrWhiteSpace(), a => a.LogLevel.Contains(searchaString) || a.LogContent.Contains(searchaString));

            result = await _logs.QueryPageAsync(expressionable.ToExpression()," ",pageIndex,pageSize);

            return new JsonResult(result);
        }
    }
}
